//
//  NearPeopleController.m
//  QSBK
//
//  Created by mac on 15/5/20.
//  Copyright (c) 2015年 Eric Wang. All rights reserved.
//
#import "UIImageView+WebCache.h"
#import "User.h"
#import "AFNetworking.h"
#import "AppDelegate.h"
#import "NearPeopleController.h"

@interface NearPeopleController ()
@property (weak, nonatomic) IBOutlet UIImageView *bigImage;
@property (weak, nonatomic) IBOutlet UIButton *addressButton;
@property (weak, nonatomic) IBOutlet UIButton *deviceButton;
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet UIButton *roleButton;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *sexImage;
@property(nonatomic,strong)User *user;
@property(nonatomic,strong)NSMutableArray *userArray;
- (IBAction)back:(id)sender;
@end

@implementation NearPeopleController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configSubview];
    [self getNearUser];
}


-(void)configSubview
{
    self.userArray = [[NSMutableArray alloc]init];
    [self.headImage.layer setCornerRadius:25];
    self.headImage.layer.masksToBounds = YES;
    
    [self.addressButton.layer setCornerRadius:15];
    self.addressButton.layer.borderWidth = 2.0;
    self.addressButton.layer.borderColor = [UIColor whiteColor].CGColor;
    
    [self.roleButton.layer setCornerRadius:15];
    self.roleButton.layer.borderWidth = 2.0;
    self.roleButton.layer.borderColor = [UIColor whiteColor].CGColor;
    
    [self.deviceButton.layer setCornerRadius:15];
    self.deviceButton.layer.borderWidth = 2.0;
    self.deviceButton.layer.borderColor = [UIColor whiteColor].CGColor;
}
-(void)getNearUser
{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    NSString *string = [NSString stringWithFormat:@"http://localhost:8888/thinkphp_3.2.3/Home/QSBK/nearuser?"];
    NSDictionary *para = @{@"user_id":delegate.user.user_id,@"longitude":delegate.user.longitude,@"latitude":delegate.user.latitude};
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:string parameters:para success:^(AFHTTPRequestOperation *operation,id responseObject){
        
        NSDictionary *dictionary = (NSDictionary *)responseObject;
        NSLog(@"%@",dictionary);
        if([[dictionary objectForKey:@"err"] intValue]==0)
        {
            NSArray *contentDic = [dictionary objectForKey:@"users"];
            for(NSDictionary *userDic in contentDic)
            {
             User *user = [[User alloc]init];
             user.last_visited_at = [userDic objectForKey:@"last_visited_at"];
             user.stater = [userDic objectForKey:@"stater"];
             user.email = [userDic objectForKey:@"email"];
             user.last_device = [userDic objectForKey:@"last_device"];
             user.role = [userDic objectForKey:@"role"];
             user.login = [userDic objectForKey:@"login"];
             user.token = [userDic objectForKeyedSubscript:@"token"];
             user.user_id = [userDic objectForKey:@"user_id"];
                user.sex = [userDic objectForKey:@"sex"];
                user.bigImage = [userDic objectForKey:@"big_image"];
             user.icon = [userDic objectForKey:@"icon"];
             user.longitude = [userDic objectForKey:@"longitude"];
             user.latitude = [userDic objectForKey:@"latitude"];
             user.articleTotal = [userDic objectForKey:@"articleTotal"];
                [self.userArray addObject:user];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self setUserInfoAtIndex:0];
            });
        }else{
            
            
            
        }
        
        
        
    }failure:^(AFHTTPRequestOperation *operation,NSError *error){
        
        NSLog(@"%@",error);
    }];
}

-(void)setUserInfoAtIndex:(int)index
{
    
    NSLog(@"%@",self.userArray);
    User *user = [self.userArray objectAtIndex:index];
    [self.bigImage sd_setImageWithURL:[NSURL URLWithString:user.bigImage] placeholderImage:[UIImage imageNamed:@"pinfo_big_bg.png"]];
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:user.icon] placeholderImage:[UIImage imageNamed:@""]];
    self.nameLabel.text = user.login;
    if([user.sex intValue]==1)
    {
        self.sexImage.image = [UIImage imageNamed:@"male.png"];
    }else{
        self.sexImage.image = [UIImage imageNamed:@"female"];
    }
    [self.addressButton setTitle:user.last_visited_at forState:UIControlStateNormal];
    [self.roleButton setTitle:user.role forState:UIControlStateNormal];
    [self.deviceButton setTitle:user.last_device forState:UIControlStateNormal];
}

- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
